package com.grsu.phonestation.web;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.grsu.phonestation.domain.Admin;
import com.grsu.phonestation.domain.Client;
import com.grsu.phonestation.domain.Invoice;
import com.grsu.phonestation.domain.TypePayment;
import com.grsu.phonestation.domain.UserActive;
import com.grsu.phonestation.service.ClientService;
import com.grsu.phonestation.service.InvoiceService;
import com.grsu.phonestation.service.TypePaymentService;
import com.grsu.phonestation.service.UserActiveService;

@Controller
@RequestMapping("/invoices")
public class InvoiceController {

	@Autowired
	private InvoiceService invoiceService;

	@Autowired
	@Qualifier("clientServiceImpl")
	private ClientService clietnService;

	@Autowired
	@Qualifier("userActiveServiceImpl")
	private UserActiveService<UserActive> userActiveService;

	@Autowired
	private TypePaymentService typePaymentService;

	@RequestMapping(value = "/replay", method = { RequestMethod.POST })
	public ModelAndView replayPayment(
			@RequestParam(value = "invoice_id", required = true) int id,
			@RequestParam(value = "metodPayment", required = true) int metodPayment,
			HttpServletRequest request, HttpSession session) {

		ModelAndView mav = new ModelAndView("redirect:/invoices");

		Invoice invoice = invoiceService.getInvoiceById(id);

		List<TypePayment> typePayments = typePaymentService.listTypePayments();

		Object userObject = session.getAttribute("user");

		if ((userObject != null) && (userObject instanceof Client)
				&& invoice != null) {

			Client user = (Client) userObject;

			TypePayment typePayment = typePaymentService.findById(metodPayment);

			invoice.setIsReplay(true);
			invoice.setTypePayment(typePayment);

			invoiceService.updateInvoice(invoice);

			List<Invoice> invoices = invoiceService.getInvoicesClient(user);

			mav.addObject("user", user);
			mav.addObject("invoices", invoices);
			mav.addObject("typePayments", typePayments);
		} else {
			mav = new ModelAndView("redirect:/login");
			mav.setViewName("login");
		}

		// mav.setViewName("invoices");

		return mav;
	}

	@RequestMapping(value = { "/", " " }, method = { RequestMethod.GET })
	public ModelAndView listInvoicesClient(HttpSession session, Model model) {

		ModelAndView mav = new ModelAndView();

		Object userObject = session.getAttribute("user");

		if (userObject == null) {

			mav = new ModelAndView("redirect:/login");
			mav.setViewName("login");
			return mav;
		}

		if (userObject instanceof Client) {

			Client user = (Client) userObject;

			List<TypePayment> typePayments = typePaymentService
					.listTypePayments();

			List<Invoice> invoices = invoiceService.getInvoicesClient(user);

			mav.addObject("user", user);
			mav.addObject("invoices", invoices);
			mav.addObject("typePayments", typePayments);
			mav.setViewName("invoices");
		} else if (userObject instanceof Admin) {
			Admin user = (Admin) userObject;

			List<Client> clients = userActiveService
					.getClientsHaveUnReplayActive();

			List<Invoice> invoices = invoiceService.listInvoices();

			mav.addObject("user", user);
			mav.addObject("invoices", invoices);
			mav.addObject("clients", clients);
			mav.setViewName("admin.invoices");
		}

		return mav;
	}

	@RequestMapping(value = "/new", method = { RequestMethod.POST })
	public ModelAndView newInvoice(@Valid @ModelAttribute Invoice invoice,
			@RequestParam(value = "userInvoice", required = true) int userId,
			HttpServletRequest request, HttpSession session) {

		ModelAndView mav = new ModelAndView("redirect:/invoices");

		Object userObject = session.getAttribute("user");

		if ((userObject != null) && (userObject instanceof Admin)) {

			Admin user = (Admin) userObject;

			List<Client> clients = userActiveService
					.getClientsHaveUnReplayActive();

			Client client = clietnService.getUserById(userId);

			invoice.setUser(client);
			invoice.setIsReplay(false);
			invoice.setSum(userActiveService.getSumUnReplayActiveClient(client));
			invoice.setDateActivation(new Date());

			invoiceService.addInvoice(invoice);

			List<Invoice> invoices = invoiceService.listInvoices();

			mav.addObject("user", user);
			mav.addObject("invoices", invoices);
			mav.addObject("clients", clients);
		} else {
			mav = new ModelAndView("redirect:/login");
			mav.setViewName("login");
		}
		return mav;
	}

}
